Skip to content

Trust resolved version over declared dep in dependency search#181

Merged
timtebeek merged 3 commits into
mainfrom
tim/cape-town-v1
May 29, 2026
Merged

Trust resolved version over declared dep in dependency search#181
timtebeek merged 3 commits into
mainfrom
tim/cape-town-v1

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented May 28, 2026

What

  • PR Match requested dependencies in ModuleHasDependency and RepositoryHasDependency #179 (ad8dbec) extended ModuleHasDependency and RepositoryHasDependency to also match against requested/declared dependencies, so they still match when dependency resolution fails (e.g. no repositories configured). As reported in rewrite-third-party#76, this introduced false positives whenever a version range is supplied:

  • If the project resolves, the resolved version is the source of truth. But the new code unconditionally also walked the declared dependencies, and the literal declared version string can satisfy the comparator even when the resolved version does not (BOM-managed versions, Gradle resolutionStrategy.force, platform alignment, etc.).

  • A null declared version (versionMatches(null, cmp)) silently matched any constraint.

Fix

    1. Trust resolved over declared. While iterating resolved dependencies, accumulate the set of resolved groupId:artifactId coordinates. When iterating requested/declared dependencies, skip any whose GA is already resolved. This preserves the "match on declared when not resolved" behaviour from Match requested dependencies in ModuleHasDependency and RepositoryHasDependency #179 while preventing the version-range false positive.
  1. Tighten versionMatches. A null declared version no longer auto-matches when a version constraint is supplied (same treatment as a ${...} property reference).

Applied identically to both ModuleHasDependency and RepositoryHasDependency.

Tests

  • Existing Match requested dependencies in ModuleHasDependency and RepositoryHasDependency #179 tests (gradleMatchesOnRequested, mavenMatchesOnRequested, gradleVersionRangeOnRequestedDoesNotMatchWhenOutOfRange) still pass.
  • New gradleVersionRangeDoesNotMatchDeclaredWhenResolvedVersionIsOutOfRange (rule 1): resolutionStrategy.force resolves out-of-range while the declared version is in-range → no match. Verified to fail without the dedup fix.
  • New gradleRequestedWithoutVersionAndConstraintDoesNotMatch (rule 2): declared dep with no version + a constraint → no match. Verified to fail without the versionMatches change.
  • Both mirrored in ModuleHasDependencyTest and RepositoryHasDependencyTest.

Note: a Maven analogue for rule 1 is not included — in Maven an explicit declared <version> on a direct dependency always wins over <dependencyManagement>/BOM, so the resolved version equals the declared version and no divergence (hence no false positive) is reproducible for a direct dep. The divergence is a Gradle phenomenon (force/platform/alignment).

./gradlew check passes.

PR #179 extended ModuleHasDependency / RepositoryHasDependency to also
match against requested/declared dependencies so they still match when
resolution fails. This introduced false positives whenever a version
range is supplied: the declared version string could satisfy the
comparator even when the resolved version did not (e.g. a Gradle
resolutionStrategy.force or platform alignment overriding the declared
coordinate).

- When a coordinate is present in the resolved dependencies, trust the
  resolved check and skip the declared-dependency fallback for that
  group:artifact. Only fall back for declared deps not already resolved.
- Tighten versionMatches so a null declared version no longer
  auto-matches when a version constraint is supplied (same treatment as
  a ${...} property reference).

Adds regression tests covering both rules in ModuleHasDependencyTest and
RepositoryHasDependencyTest.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 28, 2026
@timtebeek timtebeek marked this pull request as draft May 28, 2026 22:39
Co-authored-by: Jente Sondervorst <jentesondervorst@gmail.com>
Copy link
Copy Markdown
Contributor

@Jenson3210 Jenson3210 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the failing maven tests and this should now fix the downstream failure + other mismatches

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite May 29, 2026
@Jenson3210 Jenson3210 marked this pull request as ready for review May 29, 2026 07:20
@timtebeek timtebeek merged commit a05505c into main May 29, 2026
1 check passed
@timtebeek timtebeek deleted the tim/cape-town-v1 branch May 29, 2026 09:38
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants